#!/usr/bin/python3
# @Time    : 2018/6/3 上午 7:35:12
# @Author  : LaoJI
import datetime

from sqlalchemy import Column, ForeignKey, func
from sqlalchemy import String, Unicode, DateTime, Boolean
from sqlalchemy import SmallInteger, Integer, TIMESTAMP, and_
from sqlalchemy.orm import relationship
from app.models.base import db, Base
from app.models.myclass import MyClass
from app.models.exam_scores import Exam_Score
import time


class Exam(Base):
    __tablename__ = 'exams'
    exam_id = Column("exam_id", Integer, primary_key=True)
    title = Column("title", String(255), default=None, nullable=True)
    # teacher_id = Column("teacher_id", Integer,nullable=False)
    des_crib = Column("des_crib", String(255), default="暂时没有描述哦", nullable=True)

    degree = Column("degree", Integer, nullable=False, default=1)
    add_sub = Column("add_sub", Boolean, nullable=False, default=False)
    mul_div = Column("mul_div", Boolean, nullable=False, default=False)
    fraction = Column("fraction", Boolean, nullable=False, default=False)
    pt = Column("pt", Boolean, nullable=False, default=False)

    is_published = Column("is_published", Boolean, nullable=False, default=False)
    max_num_of_oper = Column("max_num_of_oper", Integer, default=2)
    cal_nums = Column("cal_nums", Integer, default=10)
    ques_nums = Column("ques_nums", Integer, default=10)

    beans = Column("study_pee", Integer, nullable=False, default=0)
    start_time = Column("start_time", TIMESTAMP, nullable=False)
    end_time = Column("end_time", TIMESTAMP, nullable=False)

    created_at = Column("created_at", TIMESTAMP, default=datetime.datetime.now(), nullable=True)
    updated_at = Column("updated_at", TIMESTAMP, default=datetime.datetime.now(), nullable=True)

    teacher_id = Column("teacher_id", Integer, ForeignKey('users.id'), nullable=False)
    teacher = relationship('Teacher', backref=db.backref('exams', lazy='dynamic'))

    class_id = Column("class_id", Integer, ForeignKey('classes.class_id'), nullable=False)
    myclass = relationship('MyClass', backref=db.backref('exams', lazy='dynamic'))

    @staticmethod
    def getAllExamByCid(cid, uid):
        # result = []
        # myclass = MyClass.query.get(cid)
        # myexams = myclass.exams
        # for exam in myexams:
        #     print("biewanwole1")
        result = []
        res = db.session.execute(
            "select * from exam_class where class_class_id  = :cid",
            {"cid": cid}).fetchall()
        exam_class = [{
            'exam_exam_id': es['exam_exam_id'] or '',
            'class_class_id': es['class_class_id'] or ''
        } for es in res]
        for es in exam_class:
            # flag  3表 示 考试已结束 2表示 开始 1表示没开始  4表示已经做完了
            cls_tc_msg = {}
            eid = es["exam_exam_id"]
            myexam = Exam.query.get(eid)
            print(myexam.end_time)
            exam_score = Exam_Score()
            es = Exam_Score.query.filter(and_(Exam_Score.eid == eid, Exam_Score.sid == uid)).all()
            if len(es) >0:
                cls_tc_msg["flag"] = 4
            else:
                flag = time_cmp(datetime.datetime.now(), myexam.end_time)
                if flag:
                    cls_tc_msg["flag"] = 3
                else:
                    flag = time_cmp(datetime.datetime.now(), myexam.start_time)
                    if flag:
                        cls_tc_msg["flag"] = 2
                    else:
                        cls_tc_msg["flag"] = 1

            cls_tc_msg["exam"] = myexam
            result.append(cls_tc_msg)
        return result


def time_cmp(first_time, second_time):
    return first_time > second_time
